package com.zbdx.dao.impl;

import com.zbdx.dao.IGoodsDao;
import com.zbdx.domain.product;
import com.zbdx.util.DruidUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class GoodsDaoImpl implements IGoodsDao {
    private QueryRunner qr = new QueryRunner(DruidUtil.getDataSource());
    @Override
    public int insert(product product) throws SQLException {
        String sql = "insert into product (pid,PName,PType,PNum,PPrice,mid) VALUES(NULL,?,?,?,?,?)";
        Object[] param = {product.getPName(),product.getPType(),product.getPNum(), product.getPPrice(),product.getMId()};
        int res = qr.update(sql,param);
        return res;
    }

    @Override
    public int deleteById(int PId) throws SQLException {
        String sql = "delete from product where pid = ?";
        int count = qr.update(sql,PId);
        return count;
    }

    @Override
    public int update(product product) throws SQLException {
        String sql = "update product set PName=?,PType=?,PNum=?,PPrice=? where pid=?";
        Object[] params = {product.getPName(),product.getPType(),product.getPNum(),product.getPPrice(),product.getPId()};
        int res = qr.update(sql,params);
        return res;
    }

    @Override
    public List<product> selectByType(String PType) throws SQLException {
        String sql = "select * from product where PType = ?";
        List<product> list = qr.query(sql,new BeanListHandler<product>(product.class));
        return list;
    }

    @Override
    public product selectById(int PId) throws SQLException {
        String sql = "select * from product where pid = ?";
        product product = qr.query(sql,new BeanHandler<com.zbdx.domain.product>(com.zbdx.domain.product.class),PId);
        return product;
    }

    @Override
    public List<product> search(String PName, String PType, String Id) throws SQLException {
        String sql = "SELECT * FROM product WHERE (PName LIKE ? OR PName IS NULL) "
                + "AND (PType LIKE ? OR PType IS NULL) "
                + "AND (pid LIKE ? OR pid IS NULL)";

        // 准备查询参数
        String nameParam = PName.isEmpty() ? "%" : "%" + PName + "%";
        String typeParam = PType.isEmpty() ? "%" : "%" + PType + "%";
        String idParam = Id.isEmpty() ? "%" : "%" + Id + "%";
        System.out.println(sql);
        // 执行查询
        List<product> list = qr.query(sql, new BeanListHandler<>(product.class), nameParam, typeParam, idParam);

        return list;
    }

    @Override
    public List<product> selectAll() throws SQLException {
        String sql = "select * from product";
        //调用QueryRunner的query方法进行查询
        List<product> list = qr.query(sql,new BeanListHandler<product>(product.class));
        return list;
    }
    @Override
    public int removeSelect(List<Integer> ids) throws SQLException {
        if (ids == null || ids.isEmpty()) {
            System.out.println("没有要删除的商品ID");
            return 0;
        }
        String sql = "delete from product where pid in (";
        StringBuilder placeholders = new StringBuilder();

        for (int i = 0; i < ids.size(); i++) {
            placeholders.append("?");
            if (i < ids.size() - 1) {
                placeholders.append(", ");
            }
        }
        sql += placeholders.toString() + ")";
        // 创建参数数组
        Integer[] params = ids.toArray(new Integer[0]);
        // 调用 qr.update 方法时传递参数数组
        int res = qr.update(sql, params);
        return res;
    }
}
